﻿using AxZWANPHONELib;
using System.Threading.Tasks;
using TopVisionLog;

namespace TopVisionVideoStream
{
    /// <summary>
    /// 账号登录结果回调事件
    /// </summary>
    /// <param name="isScucess"></param>
    /// <param name="msg"></param>
    public delegate void LoginEvent(bool isScucess, int msg);
    public delegate void UnRegistEventHandler();

    static class Model_Login
    {
        /// <summary>
        /// 账号登录结果回调
        /// </summary>
        private static event LoginEvent LoginResult;
        public static event UnRegistEventHandler UnRegistEvent;

        //Login
        /// <summary>
        /// 账号登录方法
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <param name="result">登录回调方法</param>
        /// <returns>操作结果</returns>
        public static bool Call(string account, string password, LoginEvent result)
        {
            var core = Configuration.Core;

            LoginResult = result;

            Configuration.UpdateAccountInformation(account, password);

            bool ret = core.bLogonServer2(account, password, password, "1.0") != 0;            

            if (!ret)
            {
                Configuration.UpdateAccountInformation("", "");
            }

            return ret;
        }
        /// <summary>
        /// 注册OCX登录事件
        /// </summary>
        public static void RegistLoginEvent()
        {
            var core = Configuration.Core;

            core.LogonResult += Core_LogonResult;

            core.RegisterSoftPhoneToSS += Core_RegisterSoftPhoneToSS;

            core.MessageInfo += Core_MessageInfo;
        }
        /// <summary>
        /// 注销OCX登录事件
        /// </summary>
        private static void UnRegistLoginEvent()
        {
            var core = Configuration.Core;

            core.LogonResult -= Core_LogonResult;

            core.RegisterSoftPhoneToSS -= Core_RegisterSoftPhoneToSS;
        }
        /// <summary>
        /// 登录成功
        /// </summary>
        /// <param name="ret"></param>
        private async static void LoginScucess(int ret)
        {
            TVLog.WriteLine("Login Scucess");

            Configuration.retryCount = 0;

            Configuration.IsLoged = true;

            Configuration.CompanyID = "";

            Configuration.CompanyKey = "";

            Configuration.UpdateCompanyInformationFormServer();

            int timer = 0;

            bool ok = true;

            while (string.IsNullOrEmpty(Configuration.CompanyID))
            {
                await Task.Delay(33);
                timer += 33;
                if (timer > 2000)
                {
                    ok = false;
                    break;
                }
            }
            if (ok)
            {
                Configuration.UpdateCompanyMembers();

                //VideoConferencManager.Init();

                VideosManager.UpdateConfVideos();

                if (LoginResult != null)
                {
                    LoginResult.Invoke(true, ret);
                }

                //TVVS.UpdateDeviceList();
            }
            else
            {
                LoginResult.Invoke(false, -1);
            }

            //VideosManager.Init();
        }
        /// <summary>
        /// 登录失败
        /// </summary>
        /// <param name="ret"></param>
        private static void LoginFaild(int ret)
        {
            TVLog.WriteLine("Login Faild");

            Configuration.retryCount = 0;

            if (LoginResult != null)
            {
                LoginResult.Invoke(false, ret);
            }

            Configuration.UpdateAccountInformation("", "");

            Model_Logout.Call(null);
        }

        /// <summary>
        /// OCX登录服务器回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">登录状态</param>
        private static void Core_RegisterSoftPhoneToSS(object sender, _DZwanPhoneEvents_RegisterSoftPhoneToSSEvent e)
        {
            int ret = e.lType;

            if (ret == 0)
            {
                Configuration.retryCount++;

                if (Configuration.retryCount > Configuration.retry)
                {
                    LoginFaild(-1);
                }
            }
            else if (ret == 1 || ret == 8)
            {
                LoginScucess(ret);
            }
            else
            {
                LoginFaild(ret);
            }
        }
        /// <summary>
        /// OCX账号验证回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">验证结果</param>
        private static void Core_LogonResult(object sender, _DZwanPhoneEvents_LogonResultEvent e)
        {
            if (LoginResult != null)
            {
                int reason = e.lReason;

                if (reason == 403)
                {
                    LoginResult.Invoke(false, reason);
                }
            }
        }

        private static void Core_MessageInfo(object sender, _DZwanPhoneEvents_MessageInfoEvent e)
        {
            if (e.strInfo == "UnRegist")
            {
                UnRegistEvent?.Invoke();
            }
        }

        public static void Dispose()
        {
            LoginResult = null;

            UnRegistLoginEvent();
        }
    }
}
